package binarysorttree;


//二叉排序树
public class BinarySortTree {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9};
        BinarySortTree tree = new BinarySortTree();
        for (int i : arr) {
            tree.add(new Node(i));
        }
        tree.infixOrder();
    }

    private Node root;

    public void add(Node node) {
        if (root != null) {
            root.add(node);
        } else {
            root = node;
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        }
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    /**
     * @param node 二叉排序树的根节点
     * @return 以node为根节点的二叉排序树的最小节点的值
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        //删除最小的节点
        delNode(target.value);
        return target.value;
    }

    /**
     * @param value
     * @return -1:树为空 0:没找到
     */
    public int delNode(int value) {
        if (root == null) {
            return -1;
        } else {
            Node targetNode = search(value);
            if (targetNode == null) {
                return 0;
            }

            //只有 root没有父节点
            if (root.left == null && root.right == null) {
                root = null;
                return 1;
                //    还有一种情况就是root是单子树的情况,也可以在后面的代码中进行判断
            } else if (root.value == value) {
                if (root.left != null || root.right == null) {
                    root = root.left;
                    return 1;
                } else if (root.right != null || root.left == null) {
                    root = root.right;
                    return 1;
                }
            }


            //找到父节点,此时一定存在父节点
            Node parent = searchParent(value);
            //如果要删除的节点是叶子节点,直接删除
            if (targetNode.left == null && targetNode.right == null) {
                if (parent.left != null && parent.left.value == value) {
                    parent.left = null;
                    return 1;
                } else {
                    parent.right = null;
                    return 1;
                }
            } else if (targetNode.left != null && targetNode.right != null) {
                //如果要删除有两颗子树的节点
                // 删除该节点右子树的最小值,并让这个最小值代替这个节点
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal;
                return 1;
                //只有一颗子树的节点
            } else {
                //如果targetNode只有左子树
                if (targetNode.left != null) {
                    //如果要删除的节点是parent的左子节点
                    if (parent.left.value == value) {
                        parent.left = targetNode.left;
                        return 1;
                    } else {
                        parent.right = targetNode.right;
                        return 1;
                    }
                    //    只有右子树
                } else {
                    //如果要删除的节点是parent的右子节点
                    if (parent.left.value == value) {
                        parent.left = targetNode.right;
                        return 1;
                    } else {
                        parent.right = targetNode.right;
                        return 1;
                    }
                }
            }


        }
    }

}

class Node {
    int value;
    Node left;
    Node right;

    /**
     * 添加节点
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value <= this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 查找到要删除的节点
     *
     * @param value
     * @return
     */
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }

    }

    /**
     * @param value
     * @return 返回要查找的父节点
     */
    public Node searchParent(int value) {
        //如果有左子节点 =value或者右子节点=value返回该节点
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value > this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }


    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}
